import * as vscode from 'vscode';
import { apiTreeDataProvider } from './ApiTreeDataProvider';
import { addUser } from './cmd/addUser';
import { fetchApiList } from './cmd/fetchApiList';
import { genApiTsFileAndOpen } from './cmd/genApiTsFile';
import { genPlaceholderDeclare } from './cmd/genPlaceholderDeclare';
import { importApi } from './cmd/importApi';
import { OpenApiDoc } from './cmd/openApi';
import { refreshApi } from './cmd/refreshApi';
import { RefreshApiTsFilesUnderDir } from './cmd/refreshApisUnderDir';
import { removeUser } from './cmd/removeUser';
import { selectOutputDir } from './cmd/selectOutputDir';
import { selectTemplateFile } from './cmd/selectTemplateFile';
import { useApi } from './cmd/useApi';
import { ApiDocViewerSerializer, viewApi } from './cmd/viewApi';
import * as configuration from './configuration';
import { Memory } from './memory';

export function activate(context: vscode.ExtensionContext) {
  console.log('[tav] activate');
  Memory.init(context);
  context.subscriptions.push(
    vscode.commands.registerCommand('torna-api-visitor.viewApi', viewApi),
    vscode.commands.registerCommand('torna-api-visitor.addUser', addUser),
    vscode.commands.registerCommand('torna-api-visitor.removeUser', removeUser),
    vscode.commands.registerCommand('torna-api-visitor.selectTemplateFile', () => selectTemplateFile().then(genPlaceholderDeclare)),
    vscode.commands.registerCommand('torna-api-visitor.selectOutputDir', selectOutputDir),
    vscode.commands.registerCommand('torna-api-visitor.fetchApiList', fetchApiList),
    vscode.commands.registerCommand('torna-api-visitor.genApi', genApiTsFileAndOpen),
    vscode.commands.registerCommand('torna-api-visitor.genaratePlaceholderDeclare', genPlaceholderDeclare),
    vscode.commands.registerCommand('torna-api-visitor.importApi', importApi),
    vscode.commands.registerCommand('torna-api-visitor.useApi', useApi),
    vscode.commands.registerCommand('torna-api-visitor.openApi', arg => OpenApiDoc.call(arg)),
    vscode.commands.registerCommand('torna-api-visitor.refreshApi', arg => refreshApi(arg)),
    vscode.commands.registerCommand('torna-api-visitor.refreshApisUnderDir', arg => RefreshApiTsFilesUnderDir.call(arg)),
  );
  vscode.window.createTreeView('torna_api_tree', { treeDataProvider: apiTreeDataProvider });
  vscode.window.registerWebviewPanelSerializer('api_doc_viewer', ApiDocViewerSerializer.get());
  configuration.getUsers().then((users) => {
    users.length && fetchApiList();
  });
}


export function deactivate() {
  console.log('[tav] deactivate');
}
